package com.ruyuan.commodity.controller.seller;

import cn.hutool.json.JSONUtil;
import com.ruyuan.commodity.enums.PermissionEnums;
import com.ruyuan.commodity.model.goods.dto.GoodsQueryParam;
import com.ruyuan.commodity.model.goods.request.GoodsInfoRequest;
import com.ruyuan.commodity.model.goods.vo.GoodsSelectLine;
import com.ruyuan.commodity.model.goods.vo.GoodsVO;
import com.ruyuan.commodity.open.api.model.common.WebPage;
import com.ruyuan.commodity.common.mamager.GoodsManager;
import com.ruyuan.commodity.common.mamager.GoodsInfoQueryManager;
import com.ruyuan.commodity.open.api.annotation.Auth;
import com.ruyuan.commodity.open.api.aspect.UserInfo;
import com.ruyuan.commodity.open.api.util.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("/seller/goods")
@Api(value = "商品相关API")
@Validated
@Scope("request")
public class GoodsInfoController {

	private static final Logger LOG = LoggerFactory.getLogger(GoodsInfoController.class);

	/**
	 * 商品信息查询相关的业务类
	 */
	@Autowired
	private GoodsInfoQueryManager goodsInfoQueryManager;
	/**
	 * 商品管理的业务类
	 */
	@Autowired
	private GoodsManager goodsManager;

	@Auth
	@ApiOperation(value = "查询商品列表")
	@GetMapping
	public WebPage<Map<String, Object>> list(GoodsQueryParam param) {
		UserInfo userInfo = AuthContextUtil.getUserInfo();
		param.setSellerId(userInfo.getSellerId());
		return goodsInfoQueryManager.list(param);
	}

	@Auth
	@ApiOperation(value = "查询预警商品列表")
	@GetMapping("/warning")
	public WebPage<GoodsVO> warningList(GoodsQueryParam param) {
		LOG.info("进入预警库存商品列表查询的接口 入参为 [{}]", JSONUtil.toJsonStr(param));
		UserInfo userInfo = AuthContextUtil.getUserInfo();
		param.setSellerId(userInfo.getSellerId());
		return goodsInfoQueryManager.warningGoodsList(param);
	}

	@Auth
	@ApiOperation(value = "添加商品")
	@PostMapping
	public GoodsVO add(@Valid @RequestBody GoodsInfoRequest request) {
		LOG.info("添加商品入参为 [{}]", JSONUtil.toJsonStr(request));
		return goodsManager.add(request);
	}

	@Auth
	@PutMapping(value = "/{id}")
	@ApiOperation(value = "修改商品")
	public GoodsVO edit(@Valid @RequestBody GoodsInfoRequest request, @PathVariable Long id) {
		LOG.info("修改商品的入参为 [{}] id为 [{}]", JSONUtil.toJsonStr(request), id);
		request.setGoodsId(id);
		return goodsManager.edit(request, id);
	}

	@Auth
	@GetMapping(value = "/{id}")
	@ApiOperation(value = "查询一个商品,商家编辑时使用")
	public GoodsVO get(@PathVariable("id") Long id) {
		return this.goodsInfoQueryManager.sellerQueryGoods(id);
	}

	@Auth
	@ApiOperation(value = "商家端下架商品",notes = "商家下架商品时使用")
	@PutMapping(value = "/{goodsIds}/under")
	public Map<String, Boolean> underGoods(@PathVariable("goodsIds") Long[] goodsIds, String reason) {
		Map<String, Boolean> result = new HashMap<>(1);
		if(StringUtils.isEmpty(reason)) {
			reason = "自行下架，无原因";
		}
		try {
			this.goodsManager.under(goodsIds,reason, PermissionEnums.SELLER);
			result.put("result", true);
			return result;
		} catch (Exception e) {
			result.put("result", false);
			return result;
		}
	}

	@Auth
	@ApiOperation(value = "商家将商品放入回收站")
	@PutMapping(value = "/{goodsIds}/recycle")
	public Map<String, Boolean> deleteGoods(@PathVariable("goodsIds") Long[] goodsIds) {
		Map<String, Boolean> result = new HashMap<>(1);
		try {
			this.goodsManager.inRecycle(goodsIds);
			result.put("result", true);
			return result;
		} catch (Exception e) {
			result.put("result", false);
			return result;
		}
	}

	@Auth
	@ApiOperation(value = "商家还原商品",notes = "商家回收站回收商品时使用")
	@PutMapping(value="/{goodsIds}/revert")
	public Map<String, Boolean> revertGoods(@PathVariable("goodsIds") Long[] goodsIds) {
		Map<String, Boolean> result = new HashMap<>(1);
		try {
			this.goodsManager.revert(goodsIds);
			result.put("result", true);
			return result;
		} catch (Exception e) {
			result.put("result", false);
			return result;
		}
	}

	@Auth
	@ApiOperation(value = "商家彻底删除商品")
	@DeleteMapping(value="/{goodsIds}")
	public Map<String, Boolean> cleanGoods(@PathVariable("goodsIds") Long[] goodsIds) {
		Map<String, Boolean> result = new HashMap<>(1);
		try {
			this.goodsManager.delete(goodsIds);
			result.put("result", true);
			return result;
		} catch (Exception e) {
			LOG.error("删除失败", e);
			result.put("result", false);
			return result;
		}
	}

	@Auth
	@GetMapping(value = "/{goodsIds}/details")
	@ApiOperation(value = "查询多个商品的基本信息")
	public List<GoodsSelectLine> getGoodsDetail(@PathVariable("goodsIds") Long[] goodsIds, GoodsQueryParam param) {
		UserInfo userInfo = AuthContextUtil.getUserInfo();
		param.setSellerId(userInfo.getSellerId());
		return this.goodsInfoQueryManager.queryGoodsLines(goodsIds, param);
	}

}
